import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import React, { useCallback, useState, useImperativeHandle, useRef, } from "react";
import { CheckBox, Form, SelectBox, TextBox } from "devextreme-react";
import { SimpleItem } from "devextreme-react/form";
import { CpasIcon, DevPopup } from "~/public/cpas-ui";
// import SubjectPopup from "~/public/cpas-business/components-bs/SubjectPopup";
import notify from "devextreme/ui/notify";
import "./_style.less";
import { AssistgetType, getAssistInfo } from "./DraftApi";
import AddPopup from "../SubjectsPopupData/components/AddPopup";
const AuxiliaryPopup = React.forwardRef((props, ref) => {
    const { AccountDataID, username, dbname } = props;
    const diaParams = useRef();
    const refsForm = useRef();
    const AddPopupRef = useRef();
    const [formAllData, setFormAllData] = useState({});
    const [visible, setVisible] = useState(false);
    const [textValue, setTextValue] = useState("");
    const [selectValue, setSelectValue] = useState("");
    // const [projectValue, setProjectValue] = useState<string>('');
    const [SelectHSXMLXBH, setSelectHSXMLXBH] = useState("");
    // const [defaultValue, setProjectValues] = useState<string>('');
    const [accountingValue, setAccountingValue] = useState([]);
    const [checkBoxState, setCheckBoxState] = useState(true);
    // 选择总账科目
    const [YSKMBH, setYSKMBH] = useState("");
    const [KMMC, setKMMC] = useState("");
    const [KMBH, setKMBH] = useState("");
    // const [AssistEditValue, setAssistEditValue] = useState<string>('')
    // const [AssistAddValue, setAssistAddValue] = useState<string>('')
    // 自定义方法
    useImperativeHandle(ref, () => ({
        show: async (params) => {
            diaParams.current = params;
            setVisible(true);
            setTextValue(params?.args?.value);
            setAccountingValue(params?.args?.items);
            setSelectValue(params?.args?.fzzData?.HSXMLXMC ?? "");
            // setCheckBoxState(!diaParams.current?.args.isEdit);
            // projectValueRef.current = diaParams.current?.args?.HSXMMC;
            setFormAllData({
                projectValue: params?.args?.HSXMMC,
            });
            setKMBH(params?.args?.fzzData.KMBH);
            if (!params?.args?.edit) {
                setKMMC(params?.args?.fzzData.KMMC);
                setYSKMBH(params?.args?.fzzData.YSKMBH);
            }
        },
        close: () => {
            setVisible(false);
        },
    }));
    function EditSate(r) {
        const md5 = require("md5");
        const projectValues = formAllData.projectValue;
        const projectValue = formAllData.projectValue;
        if (r) {
            // 编辑
            diaParams.current?.okCallback({
                textValue,
                projectValue,
                selectValue,
                SelectHSXMLXBH,
                projectValues,
            });
            setVisible(false);
        }
        else {
            // 增加
            const projectValueMds = md5(projectValues);
            if (projectValues.length > 0) {
                const reg = /^[\s\S]*.*[^\s][\s\S]*$/;
                if (reg.test(projectValues)) {
                    diaParams.current?.okCallback({
                        textValue,
                        projectValue,
                        selectValue,
                        SelectHSXMLXBH,
                        projectValues,
                        projectValueMds,
                        YSKMBH,
                        KMBH,
                        KMMC,
                        checkBoxState,
                    });
                    // setProjectValue('')
                    setVisible(false);
                }
                else {
                    notify("请输入核算项目名称", "warning", 2000);
                }
            }
            else {
                notify("请输入核算项目名称", "warning", 2000);
            }
        }
    }
    const onOk = useCallback(async () => {
        if (diaParams.current?.okCallback) {
            const projectValue = formAllData.projectValue;
            // 拼接科目编号
            const r = await getAssistInfo({
                dbname: dbname,
                Data_ID: AccountDataID,
                HSXMLXBH: SelectHSXMLXBH,
                KMBH,
                HSXMMC: projectValue,
            });
            if (r.data?.length)
                return notify("该科目下已存在相同名称的辅助核算项目，无法重复新增！", "error", 2000);
            EditSate(diaParams.current?.args?.edit);
        }
    }, [
        textValue,
        selectValue,
        SelectHSXMLXBH,
        YSKMBH,
        KMBH,
        KMMC,
        checkBoxState,
        formAllData,
    ]);
    const onCancel = () => {
        setVisible(false);
        // setCheckBoxState(true);
        setFormAllData({
            projectValue: "",
        });
        if (diaParams.current?.cancelCallback) {
            diaParams.current?.cancelCallback();
        }
    };
    const changeCheck = useCallback((v) => {
        setCheckBoxState(v);
    }, [checkBoxState]);
    return (_jsxs(_Fragment, { children: [_jsx(DevPopup.Logic, { visible: visible, title: diaParams.current?.args?.title, width: 600, height: 300, onOK: () => onOk(), onCancel: () => {
                    onCancel();
                }, onHiding: () => {
                    onCancel();
                }, children: () => {
                    return (_jsxs("div", { style: { width: "100%", height: "100%" }, className: "auxiliaryPopupBox", children: [diaParams.current?.args?.edit ? (_jsxs(Form, { ref: refsForm, colCount: 2, formData: formAllData, labelLocation: "left", labelMode: "outside", focusStateEnabled: true, showColonAfterLabel: true, alignItemLabels: true, className: "SubjectsForm", children: [_jsxs(SimpleItem, { colSpan: 2, label: { text: "总 账 科 目" }, children: [_jsx(TextBox, { value: textValue, disabled: true }), _jsx("span", { style: { position: "absolute", right: "6px", top: "6px" }, children: _jsx(CpasIcon, { type: "icon-baobiaoxiangmubianji" }) })] }), _jsx(SimpleItem, { colSpan: 2, label: { text: "核算项目类型" }, children: _jsx(SelectBox, { dataSource: accountingValue, disabled: true, value: selectValue
                                                ? selectValue
                                                : accountingValue[0]?.HSXMLXMC ?? "", displayExpr: "HSXMLXMC", valueExpr: "HSXMLXMC", onSelectionChanged: (e) => {
                                                setSelectHSXMLXBH(e.selectedItem.HSXMLXBH);
                                                setSelectValue(e.selectedItem.HSXMLXMC);
                                            } }) }), _jsx(SimpleItem, { label: { text: "核算项目名称" }, colSpan: 2, dataField: "projectValue", editorOptions: {
                                            valueChangeEvent: "keyup",
                                        } })] })) : (_jsxs(Form, { ref: refsForm, colCount: 2, formData: formAllData, labelLocation: "left", labelMode: "outside", focusStateEnabled: true, showColonAfterLabel: true, alignItemLabels: true, className: "SubjectsForm", children: [_jsxs(SimpleItem, { colSpan: 2, label: { text: "总 账 科 目" }, children: [_jsx(TextBox, { value: textValue, className: "TextBox_Style" }), _jsx("span", { style: { position: "absolute", right: "6px", top: "6px" }, children: _jsx(CpasIcon, { type: "icon-baobiaoxiangmubianji", onClick: () => {
                                                        AddPopupRef.current.show({
                                                            title: "选择科目",
                                                            dbname: dbname,
                                                            Data_ID: AccountDataID,
                                                            isHasFZZ: false,
                                                            selectMode: "0",
                                                            editUser: username,
                                                            // 判断 是否为辅助账
                                                            auxiliaryState: diaParams.current?.args?.auxiliaryState,
                                                            // 选择 模式 0：单选 1：多选连机选择
                                                            okCallback: async (r, v) => {
                                                                console.log(r, v, "r------rrrrrrrrr");
                                                                const res = await AssistgetType({
                                                                    dbname: dbname,
                                                                    Data_ID: AccountDataID,
                                                                    KMBH: r.KMBH,
                                                                });
                                                                // const PKMBH = r.PKMBH === '' ? '' : r[0].PKMBH ? handleStr(r[0].PKMBH) : handleStr(r.PKMBH);
                                                                // const KMMC = r.KMMC ? r.KMMC : r[0].KMMC;
                                                                const KMMC = r.displayName
                                                                    ? r.displayName
                                                                    : r[0]?.YSKMBH ?? "" + "-" + r[0]?.YSKMMC ?? "";
                                                                // 获取选中的数据
                                                                setKMBH(r.KMBH ? r.KMBH : r[0]?.KMBH ?? "");
                                                                setKMMC(r.KMMC ? r.KMMC : r[0]?.KMMC ?? "");
                                                                setYSKMBH(r.YSKMBH ? r.YSKMBH : r[0]?.YSKMBH ?? "");
                                                                setTextValue(KMMC);
                                                                setAccountingValue(res);
                                                            },
                                                        });
                                                    } }) })] }), _jsx(SimpleItem, { colSpan: 2, label: { text: "核算项目类型" }, children: _jsx(SelectBox, { dataSource: accountingValue, value: selectValue ? selectValue : accountingValue[0]?.HSXMLXMC, displayExpr: "HSXMLXMC", valueExpr: "HSXMLXMC", onSelectionChanged: (e) => {
                                                setSelectHSXMLXBH(e.selectedItem.HSXMLXBH);
                                                setSelectValue(e.selectedItem.HSXMLXMC);
                                            } }) }), _jsx(SimpleItem, { label: { text: "核算项目名称" }, colSpan: 2, dataField: "projectValue", editorOptions: {
                                            valueChangeEvent: "keyup",
                                        } })] })), !diaParams.current?.args.edit && (_jsxs("div", { className: "isAddMoreYear", children: [_jsx("span", { style: {
                                            marginRight: "10px",
                                            whiteSpace: "nowrap",
                                        }, children: "\u662F\u5426\u589E\u52A0\u591A\u5E74\u5EA6:" }), _jsx(CheckBox, { value: checkBoxState, onValueChange: (e) => {
                                            // setCheckBoxState(e);
                                            changeCheck(e);
                                        } })] }))] }));
                } }), _jsx(AddPopup, { ref: AddPopupRef })] }));
});
export default AuxiliaryPopup;
